package Servlet;

import dao.AddPlace;
import dao.QueryPlaceName;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class addPlaceServlet extends HttpServlet {

    String placeName;
    String imageUrl;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.print(getfile(request));
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }

    private String getfile(HttpServletRequest req) {
        String message = "0";
        int n = 1;
        try {
            req.setCharacterEncoding("UTF-8");
            byte[] buffer = new byte[1024];
            DiskFileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload sUpload = new ServletFileUpload(factory);
            List<FileItem> list = sUpload.parseRequest(req);
            for (FileItem item : list) {
                String name = item.getFieldName();
                System.out.println(name);
                if (item.get().length == 0) {
                    break;
                }
                if (name.equals("image")) {
                    InputStream in = item.getInputStream();
                    String image = item.getContentType();
                    String type = image.substring(image.lastIndexOf('/') + 1);
                    UUID uuid = UUID.randomUUID();
                    imageUrl = uuid.toString() + "." + type;
                    File file = new File("C:\\Users\\19615\\Desktop\\WebApplication11\\web\\placeImage\\" + imageUrl);
                    if (!file.exists()) {
                        file.createNewFile();
                    }
                    BufferedInputStream fi = new BufferedInputStream(in);
                    FileOutputStream fo = new FileOutputStream(file);
                    int length;
                    while ((length = fi.read(buffer)) != -1) {
                        fo.write(buffer, 0, length);
                    }
                    fo.flush();
                    fo.close();
                    fi.close();
                    in.close();
                } else {
                    placeName = item.getString("UTF-8");
                    n = QueryPlaceName.getResult(placeName);
                    if (n == 1) {
                        break;
                    }
                }
            }
            if (n == 0) {
                AddPlace.add(placeName, imageUrl);
                message = "1";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return message;
    }
}
